<?php
declare (strict_types = 1);

namespace app\index\controller;
use think\facade\View;
use think\facade\Db;
use think\captcha\facade\Captcha;
use think\facade\Cache;

use app\common\model\Category as CategoryModel;
use app\common\model\Lists as ListsModel;
use app\common\model\Contact as ContactModel;
use app\common\model\ListsComment as ListsCommentModel;
use app\common\model\Order as OrderModel;

use app\admin\validate\Contact as validateContact;

class Index extends Base
{
    function __construct(){

        parent::__construct();
    }


    public function index()
    {
        //轮播图
        //$banner_res = DB::name("banner")->where("is_open",'=',1)->order("orderid desc")->select();

        //View::assign('banner_res',$banner_res);
        return View::fetch();
    }

    public function lists(){
        $param = input("param.");
        $catid = $param['catid'];
        $cate_res = $this->common($catid);

        //template_list 页面为空，找最上级的页面
        if($cate_res['template_list'] == ''){
            $pid = getPid($catid);
            $CategoryModel = new CategoryModel();
            $father_one = $CategoryModel->where('id','=',$pid)->find();
            $cate_res['template_list'] = $father_one['template_list'];
        }

        return View::fetch($cate_res['template_list']);
    }

    public function common($catid){
        //$data = input('param.');
        $cate_res =DB::name('category')->where('id',$catid)->find();
        //dump($cate_res);
        $pid = getPid($catid);//当前的顶级id
        $father_one = DB::name("category")->where('id','=',$pid)->find();
        View::assign('father_one',$father_one);

        $child_one_res = DB::name('category')->where('parentid',$pid)->order('orderid asc')->select();
        //有二级分类
        if($cate_res['module'] == 'lists'){
            $level = count(explode(',',$cate_res['arrparentid']));
            if($level == 1){
                $child_one_str = '';
                foreach($child_one_res as $k=>$v){
                    $child_one_str.=$v['id'].',';
                }
                $child_one_str = trim($child_one_str,',');

            }else if($level == 2){
                $child_one_str = $cate_res['id'];
            }else if($level == 3){
                $child_one_str = $cate_res['parentid'];
            }
            if($child_one_str){
                $child_two_res = DB::name('category')->where('parentid','in',$child_one_str)->order('orderid asc')->select();
            }else{
                $child_two_res = [];
            }
            View::assign('level',$level);//当前的上一级id
            View::assign('parentid',$cate_res['parentid']);//当前的上一级id
            View::assign('child_two_res',$child_two_res);
        }

        //关于我们
        if($cate_res['module'] == 'info'){
            $guanyu = DB::name($cate_res['module'])
                ->where('is_open','=',1)
                ->order('orderid asc')
                ->limit(1)
                ->select();
            View::assign('guanyu',$guanyu);

            $lianxi = DB::name($cate_res['module'])
                ->where('is_open','=',1)
                ->order('orderid asc')
                ->limit(1,1)
                ->select();
            View::assign('lianxi',$lianxi);
        }

        $cate_ids = getChildId($cate_res['id'],true);
        //列表
        $pagesize = $cate_res['pagesize']?$cate_res['pagesize']:10;
        $res = DB::name($cate_res['module'])
            ->where('status','=',1)
            ->where('catid','in',$cate_ids)->order('orderid desc')->paginate([
                'list_rows'=> $pagesize,//每页数量
                'query' => request()->param(),
            ])->each(function($item, $key){
                $c_one = DB::name('category')->where('id','=',$item['catid'])->find();
                $item['cate_name'] = $c_one['catname'];
                return $item;
            });

        $page = $res->render();
        View::assign('catid',$catid);
        View::assign('pid',$pid);
        View::assign('page',$page);
        View::assign('res',$res);
        View::assign('child_one_res',$child_one_res);
        //return View::fetch($cate_res['template_list']);

        return $cate_res;
    }

    //空方法
    public function __call($name, $arguments){
        $c_name_one = DB::name('category')->where("htmlname","=",$name)->find();

        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :$c_name_one['id'];
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function course(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :75;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function special(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :78;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function skills(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :79;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function safe(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :80;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }


    public function title(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :86;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function policydetail(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :94;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function contact(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :99;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function about(){
        $param = input("param.");
        $catid = isset($param['catid']) ? $param['catid'] :100;
        $cate_res = $this->common($catid);
        return View::fetch($cate_res['template_list']);
    }

    public function detail(){
        $param = input('param.');
        $id = $param['id'];

        $cate_res =DB::name('category')->where('id','=',$param['catid'])->find();
        $info = DB::name($cate_res['module'])->where('id','=',$param['id'])->find();


        if($cate_res['module'] == 'policy'){
            //随机推荐两条
            $rand_res = DB::name($cate_res['module'])
                ->where('is_open','=',1)
                ->where('id','<>',$param['id'])
                ->orderRaw('rand()')->limit(2)->select()->toArray();
            foreach($rand_res as $k=>$v){
                $c_one = DB::name('category')->where('id','=',$v['cate_id'])->find();
                $rand_res[$k]['cate_name'] = $c_one['catname'];
            }
            View::assign('rand_res',$rand_res);
        }
        //评论
        if($cate_res['module'] == 'lists'){
            $effective_date = $info['effective_date'];
            if(isset($info['effective_date']) && $info['effective_date'] != '') {
                $info['effective_date'] = date("Y年m月d日", strtotime($info['effective_date']));
            }

            $ListsCommentModel = new ListsCommentModel();
            $param['lists_id'] = $id;
            $param['limit'] = 5;
            $res = $ListsCommentModel -> tableData($param);

            View::assign('comment_res',$res['data']);
            View::assign('page',$res['page']);
            View::assign('pagetotal',$res['pagetotal']);

            //超过截止日期不显示
            $is_baoming_show = 1;
            if($info['type'] == 1){
                if($effective_date < date('Y-m-d',time())){
                    $is_baoming_show = 2;
                }
            }
            View::assign('is_baoming_show',$is_baoming_show);

            //报名成功的才有显示框
            $is_show_comment = 2;
            if(cookie('user_id')) {
                $OrderModel = new OrderModel();
                $ord_one = $OrderModel
                    ->where('user_id', '=', cookie('user_id'))
                    ->where('pay_status', '=', 3)
                    ->where('lists_id', '=', $id)
                    ->find();
                if($ord_one){
                    $is_show_comment = 1;
                }
            }

            View::assign('is_show_comment',$is_show_comment);
        }

        //上一页
        $prev  = DB::name($cate_res['module'])->where('cate_id',$param['catid'])->where('orderid', '>',$info['orderid'])->order('orderid asc')->find();
        //下一页
        $next = DB::name($cate_res['module'])->where('cate_id',$param['catid'])->where('orderid', '<',$info['orderid'])->order('orderid desc')->find();


        View::assign('info',$info);
        View::assign('cate_res',$cate_res);

        View::assign('prev',$prev);
        View::assign('next',$next);
        return View::fetch($cate_res['template_show']);
    }

    public function getListsComment(){
        $param = input("param.");
//        return dyajaxReturn(1,'22',$param);
        $ListsCommentModel = new ListsCommentModel();
        $param['limit'] = 5;
        $res = $ListsCommentModel -> tableData($param);

        return dyajaxReturn(1,'22',$res);
    }

    //提交评论
    public function addListsComment(){
        $param = input("param.");
        if($param['content'] == ''){
            return dyajaxReturn(0,'评价内容不能为空');
        }
        if($param['lists_id'] == ''){
            return dyajaxReturn(0,'lists_id不能为空');
        }
        if(!cookie('user_id')){
            return dyajaxReturn(0,'请先登录');
        }

        //过滤敏感词
        $res = filterate($param['content']);
        if($res['code'] == 0){
            return dyajaxReturn(0,$res['msg']);
        }

        $ListsCommentModel  = new ListsCommentModel();
        $param['act'] = 'add';
        $res = $ListsCommentModel->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    //获取验证码
    public function vertify(){
        return Captcha::create('verify');
    }

    //提交联系我们
    public function addContact(){
        $param = input("param.");

        $validate = new validateContact;
        $result = $validate->scene('add')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }
        $vertify = $param['vertify'];
        if( !captcha_check($vertify)) {
            return dyajaxReturn(0,'验证码错误');
        }

        //过滤敏感词
        $res = filterate($param['content']);
        if($res['code'] == 0){
            return dyajaxReturn(0,$res['msg']);
        }

        $model = new ContactModel();
        $res = $model -> handle($param);
        return dyajaxReturn($res['code'],$res['msg']);
    }


    public function search(){
        $param = input("param.");

        $model = new ListsModel();
        $param['is_open'] = 1;
        $param['keyword'] = $param['keyword'];
        $res = $model->tableData($param);

        View::assign('page',$res['page']);
        View::assign('res',$res['data']);
        View::assign('total',$res['count']);
        return View::fetch('index/search');
    }

    /*
     * 图片上传
     * @param $filenames  文件域
     * @param $position  存放目录
    */
    public function dyupload(){

        return dyuploadimg('dypic',input('imgpath/s'));
    }

    /*
     * 文件上传
     * @param $filenames  文件域
     * @param $position  存放目录
    */
    public function dyuploadfile(){
        return dyuploadfile('file',input('imgpath/s'));
    }


    //视频上传
    public function dyuploadVideo()
    {
        return dyuploadVideo('dyvideo',input('imgpath/s'));
    }

}
